package com.dd.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.reggie.common.BaseContext;
import com.dd.reggie.dto.OrdersDto;
import com.dd.reggie.entity.*;
import com.dd.reggie.mapper.OrdersMapper;
import com.dd.reggie.service.*;
import com.google.gson.JsonArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    private OrdersDetailService ordersDetailService;

    @Autowired
    private UserService userService;

    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Transactional
    public void submit(Orders orders) {

        //生成订单号/id
        long id = IdWorker.getId();
        //获取用户信息
        User user = userService.getById(BaseContext.getCurrentId());
        //获取地址等信息
        AddressBook addressBook = addressBookService.getById(orders.getAddressBookId());

        //获取购物车列表
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, user.getId());
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);

        AtomicInteger amount = new AtomicInteger();

        List<OrderDetail> orderDetails = list.stream().map((item) -> {

            OrderDetail orderDetail = new OrderDetail();

            orderDetail.setOrderId(id);
            orderDetail.setNumber(item.getNumber());
            orderDetail.setDishFlavor(item.getDishFlavor());
            orderDetail.setDishId(item.getDishId());
            orderDetail.setSetmealId(item.getSetmealId());
            orderDetail.setName(item.getName());
            orderDetail.setImage(item.getImage());
            orderDetail.setAmount(item.getAmount());

            //将单价乘以总数得到每个菜品的价格，再加起来
            amount.addAndGet(item.getAmount().multiply(new BigDecimal(item.getNumber())).intValue());

            return  orderDetail;
        }).collect(Collectors.toList());

        //修改订单信息
        orders.setId(id);
        orders.setNumber(String.valueOf(id));
        orders.setStatus(2);
        orders.setUserId(user.getId());
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setAmount(new BigDecimal(amount.get()));
        orders.setPhone(addressBook.getPhone());
        orders.setUserName(user.getName());
        orders.setConsignee(addressBook.getConsignee());
        orders.setAddress((addressBook.getProvinceName() == null ? "" : addressBook.getProvinceName())
                + (addressBook.getCityName() == null ? "" : addressBook.getCityName())
                + (addressBook.getDistrictName() == null ? "" : addressBook.getDistrictName())
        );



        //保存
        ordersDetailService.saveBatch(orderDetails);

        this.save(orders);

        //清空购物车
        shoppingCartService.remove(queryWrapper);

    }
}
